package com.mis.clothespos.sale.controller;


import com.mis.clothespos.clothes.domain.clothes;
import com.mis.clothespos.clothes.service.IclothesService;
import com.mis.clothespos.common.AjaxResult;
import com.mis.clothespos.common.BaseController;
import com.mis.clothespos.in.domain.In;
import com.mis.clothespos.sale.domain.*;
import com.mis.clothespos.sale.service.saleService;
import com.mis.clothespos.shiro.common.ShiroUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmIndexes.UUID;

@Controller
@RequestMapping("/sale")
public class saleController extends BaseController{

    @Autowired
    saleService s_service;
    @Autowired
    IclothesService iclothesService;




    @RequestMapping(value ="/addSale")
    @ResponseBody
    public AjaxResult addSale(@RequestBody VueOrder vueorder){
        //提取vue前端传来的类
        List<Orederitem> item=vueorder.getItem();
        Order order=new Order();
        Payment payment=new Payment();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        String orderId=df2.format(new Date());
        Date orderTime=new Date();
        order.setId(orderId);
        order.setCostprice(vueorder.getCostPrice());
        order.setPayprice(vueorder.getPayPrice());
        order.setTime(orderTime);
        order.setPayway(vueorder.getPaymethod());
        order.setCteatby(ShiroUtils.getSysUser());
        //插入订单类
        s_service.inserOrder(order);
        //插入支付表
        payment.setOrderid(orderId);
        payment.setPrice(vueorder.getCostPrice());
        payment.setPaymethod(vueorder.getPaymethod());
        payment.setTime(orderTime);
        s_service.insertPayTable(payment);
        for(int i=0;i<item.size();i++){
            Orederitem orderitem=item.get(i);
            String productId=orderitem.getId();
            long previousPrice = orderitem.getPrice().longValue();
            double paypd = Double.parseDouble(vueorder.getPayPrice());
            double costpd = Double.parseDouble(vueorder.getCostPrice());
            double subsPriced = costpd/paypd*previousPrice;
            BigDecimal bd=new BigDecimal(subsPriced).setScale(0, BigDecimal.ROUND_HALF_UP);
            long subsPrice = Long.parseLong(bd.toString());
            //long subsPrice = costp/payp*previousPrice;
            Long longSubsPrice = new Long(subsPrice);
            orderitem.setPrice(longSubsPrice);
            orderitem.setTotalprice(orderitem.getPrice()*orderitem.getNumber());
            orderitem.setId(RandomStringUtils.randomAlphanumeric(10));
            clothes c=new clothes();
            c.setId(Integer.valueOf(productId));
            c.setNumber(iclothesService.selectById(Integer.valueOf(productId)).getNumber()-orderitem.getNumber());
            iclothesService.updateByPrimaryKeySelective(c);
            //插入订单条目
            s_service.insertSale(orderitem);
            OrderToItem orderToItem=new OrderToItem();
            orderToItem.setOrderid(order.getId());
            orderToItem.setOrderitemid(orderitem.getId());
            //关联中间表
            s_service.insertOrderToItem(orderToItem);
        }
        AjaxResult result =new AjaxResult();
        result.put("code",200);
        result.put("msg","销售成功");
        return  result;


    }
}
